import sys
import os
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(parent_dir)

import time
from driving_route_planning import driving_route_planning
from generate_map import generate_map
import pickle
from utils.tools import read_json

if __name__ == "__main__":
    # 配置信息
    config = read_json("path_planning/config.jsonc")
    AK = config["AK"]
    SK = config["SK"]
    origin_list = [
        '29.62849,106.484789',                          #重庆两江新区中医院
        '29.527289,106.528108',                         #重庆医科大学附属第一医院建设医院
        '29.562082,106.472457',                         #重庆市沙坪坝区人民医院
        '29.543099,106.458983',                         #陆军军医大学第一附属医院(重庆西南医院)
        '29.527258,106.579794',                         #南岸区人民医院
        '29.375306,106.545661',                         #重庆市巴南区人民医院
        '29.556116,106.30944'                           #重庆高新区第一人民医院
    ]
    names = ["重庆两江新区中医院", 
             "重庆医科大学附属第一医院建设医院",
             "重庆市沙坪坝区人民医院",
             "陆军军医大学第一附属医院(重庆西南医院)",
             "南岸区人民医院",
             "重庆市巴南区人民医院",
             "重庆高新区第一人民医院"]
    
    destination = '29.612624,106.338693'                #沙磁科技园
    destination_name = "沙磁科技园"

    map_output_path = config["map_output_path"]
    data_output_path = config["data_output_path"]

    start = time.time()
    datas = []
    for origin in origin_list:
        data = driving_route_planning(AK, SK, origin, destination)
        datas.append(data)
    end = time.time()

    print ("路径规划总调用时间", end - start)
    print ("路径规划总调用次数", len(origin_list))

    coordinates_list = []
    for distance, duration, score, paths in datas:
        tmp0 = paths.split("|")
        tmp1 = []
        for x in tmp0:
            tmp1 += x.split(";")
        tmp2 = []
        for x in tmp1:
            tmp2.append([float(x.split(",")[1]), float(x.split(",")[0])])
        coordinates = tmp2
        coordinates_list.append(coordinates)
    generate_map(coordinates_list, names, map_output_path)
    with open(data_output_path, "wb") as haddle:
        pickle.dump((names, destination_name, origin_list, destination, coordinates_list, datas), haddle)

    